<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <el-tab-pane label="登录账户设置">

            <!-- 放置表单 -->
            <el-form ref="form" :model="formData" :rules="rules" label-width="120px" style="margin-left: 120px; margin-top:30px">
              <el-form-item label="姓名:" prop="username">
                <el-input v-model="formData.username" style="width:300px" />
              </el-form-item>
              <el-form-item label="新密码:" prop="password">
                <el-input v-model="formData.password" style="width:300px" show-password type="password" />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="submit">更新</el-button>
              </el-form-item>
            </el-form>
          </el-tab-pane>
          <el-tab-pane label="个人详情">
            <components :is="UserInfo" :user-id="userId" />
          </el-tab-pane>
          <el-tab-pane label="岗位信息">
            <components :is="JobInfo" :user-id="userId" />
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>

<script>
import UserInfo from '@/components/UserInfo/index.vue'
import JobInfo from '@/components/JobInfo'
import { getUserDetailById } from '@/api/user'
import { SaveuserDetails } from '@/api/employees'

export default {
  components: {
    UserInfo,
    JobInfo
  },
  data() {
    return {
      JobInfo,
      UserInfo,
      userId: this.$route.params.id,
      formData: {
        username: '',
        password: ''
      },
      rules: {
        username: [{ required: true, message: '姓名不能为空', trigger: 'blur' }],
        password: [{ required: true, message: '密码不能为空', trigger: 'blur' },
          { min: 6, max: 9, message: '密码长度6-9位', trigger: 'blur' }]
      }
    }
  },
  async  created() {
    await this.render()
  },
  methods: {
    async render() {
      const { data } = await getUserDetailById(this.userId)
      this.formData = { ...data, password: '' }
    },
    async submit() {
      try {
        await this.$refs.form.validate()
        await SaveuserDetails(this.formData)
        this.$message.success('更新成功')
        await this.render()
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>
